/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package snAnalysis;

import base.Utilities;
import debug.DebugFrame;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.StringTokenizer;
import wave.WaveInterpreter;

/**
 *
 * @author FUJISAI
 */
public class MakeBlogKN {

    private int limitDB = 0;
    private String localIPAddress;
    private String table;
    private String filter_method;

    public MakeBlogKN() {  
        this.limitDB        = Integer.parseInt(Configuration.getValue("limitDB", "4000"));
        this.localIPAddress = Configuration.getValue("localIP", "192.168.1.111");
        this.table          = Configuration.getValue("table_name", "blog_list");
        this.filter_method  = Configuration.getValue("filter_method", "dbSorting");      
    }

    public void ShortWave(MakeBlogKN blog, WaveInterpreter win){
         try {
            MySQLBridge         mysql   = new MySQLBridge();
            Filter              bF      = new Filter(this.filter_method);
            GenerateWaveCode    gen     = new GenerateWaveCode(false);
            ResultSet rel = mysql.customQuery("SELECT COUNT(`id`) AS TOTAL FROM `" + this.table + "`");
            int size = 0;
            while (rel.next()) {
                size = rel.getInt("TOTAL");
            }
            System.out.println(size);
            for ( int i=0;i<(size/this.limitDB)+1;i++) {
                rel = mysql.getAllField(this.table, "1 LIMIT " + i*this.limitDB + ", " + this.limitDB);
                while (rel.next()) {
                    String blogID   = rel.getString("blogID");
                    String friends  = rel.getString("friends");

                    StringTokenizer tokens = new StringTokenizer( friends );
                    String destIPBlog = bF.getOutputIP(blogID);
                    
                    while ( tokens.hasMoreTokens() ) {
                        gen.initWaveS(blogID, destIPBlog, blog.localIPAddress);

                        String temp = tokens.nextToken(";");
                        String destIP = bF.getOutputIP(temp);

                        gen.appendWaveS(temp, destIPBlog, destIP);

                        win.injectWave(gen.getWaveString());
                    }
                    /** Make KN even for the non-friends blogID */
                    if (friends.compareTo("") == 0) {
                        gen.initWaveS(blogID, destIPBlog, blog.localIPAddress);
                        win.injectWave(gen.getWaveString());
                    }
                }
            }
            mysql.closeConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public void LongWave(MakeBlogKN blog, WaveInterpreter win){
         try {

            MySQLBridge         mysql   = new MySQLBridge();
            Filter              bF      = new Filter(this.filter_method);
            GenerateWaveCode    gen     = new GenerateWaveCode(false);

            ResultSet res = mysql.customQuery("SELECT COUNT(`id`) AS TOTAL FROM `" + this.table + "`");
            int size = 0;
            while (res.next()) {
                size = res.getInt("TOTAL");
            }
            for ( int i=0;i<(size/this.limitDB)+1;i++) {
                ResultSet rel = mysql.getAllField(this.table, "1 LIMIT " + i*this.limitDB + ", " + this.limitDB);
//                ResultSet rel = mysql.getAllField("blog_list", "");
                while (rel.next()) {
                    String blogID = rel.getString("blogID");
                    String friends = rel.getString("friends");

                    String destIPblog = bF.getOutputIP(blogID);
                    gen.initWaveS(blogID, destIPblog, blog.localIPAddress);

                    StringTokenizer tokens = new StringTokenizer( friends );
                    while ( tokens.hasMoreTokens() ) {
                        String temp = tokens.nextToken(";");

                        String destIP = bF.getOutputIP(temp);
                        gen.appendWaveS(temp, destIPblog, destIP);
                    }
                    /**
                     * Inject WAVE code with one blogID and his friends
                     */
                    win.injectWave(gen.getWaveString());
}
            }
            mysql.closeConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static void main(String args[]) {
        MakeBlogKN blog = new MakeBlogKN();
        WaveInterpreter win = WaveInterpreter.getInstance();
        
        Utilities.debugMode = true;
        Utilities.MULTI_THREAD=true;
//        DebugFrame db=new DebugFrame();
        win.start();

        blog.LongWave(blog, win);

        while(true){
            System.out.println("So node: " + win.getKN().getNumberNodes());
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}
